package cn.dgut.training.entity.dto;

import cn.dgut.training.dto.BaseDTO;
import cn.dgut.training.entity.transaction.GroupJoin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class GroupJoinDTO extends BaseDTO {

    @ApiModelProperty(value = "参与表的ID")
    private Long id;

    @ApiModelProperty(value = "用户id")
    private Long userId;

    @ApiModelProperty(value = "活动id")
    private Long activityId;

    @ApiModelProperty(value = "订单id")
    private Long orderId;

    public static QueryWrapper getWrapper(GroupJoinDTO groupJoinDTO) {

        if (groupJoinDTO.getOrderBy() == null) {
            groupJoinDTO.setSort("desc");
        }
        if (groupJoinDTO.getSort() == null) {
            groupJoinDTO.setSort("add_time");
        }

        QueryWrapper queryWrapper = new QueryWrapper();

        if (groupJoinDTO.getActivityId() != null) {
            queryWrapper.eq("activity_id", groupJoinDTO.getActivityId());
        }

        if (!StringUtils.isBlank(groupJoinDTO.getOrderBy())) {
            if ("desc".equals(groupJoinDTO.getSort())) {
                queryWrapper.orderByDesc(groupJoinDTO.getOrderBy());
            } else {
                queryWrapper.orderByAsc(groupJoinDTO.getOrderBy());
            }
        }

        return queryWrapper;
    }
}
